package com.xunsiya.controller.device;

import com.xunsiya.common.config.RequestUtils;
import com.xunsiya.module.device.vo.Device;
import com.xunsiya.service.module.device.DeviceService;
import com.xunsiya.system.vo.Company;
import com.xunsiya.system.vo.User;
import com.xunsiya.tools.common.pagination.ForePage;
import com.xunsiya.tools.common.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xunsiya.tools.common.msg.Msg;

import java.util.List;

/**
 * 设备管理Controller
 *
 * @author jiax
 * @version 1.0
 * <p>
 * <p>修订人		修订时间			描述信息
 * <p>-----------------------------------------------------
 * <p>jiax		2018/11/6		初始创建
 */
@RestController
@RequestMapping("/api/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 分页查询
     *
     * @param device 设备信息
     * @return
     */
    @PostMapping
    public ForePage<Device> findPage(@RequestBody Device device) {
        int pageIndex = RequestUtils.getIntParam("pageIndex") == null ? 1
                : RequestUtils.getIntParam("pageIndex");
        int pageSize = RequestUtils.getIntParam("pageSize") == null ? 10
                : RequestUtils.getIntParam("pageSize");
        return deviceService.findPage(device, pageIndex, pageSize);
    }

    /**
     * 分页查询
     *
     * @param device 设备信息
     * @return
     */
    @PostMapping("findAll")
    public ForePage<Device> findAll(@RequestBody Device device) {
        int pageIndex = RequestUtils.getIntParam("pageIndex") == null ? 1
                : RequestUtils.getIntParam("pageIndex");
        int pageSize = RequestUtils.getIntParam("pageSize") == null ? 10
                : RequestUtils.getIntParam("pageSize");
        return deviceService.findAll(device, pageIndex, pageSize);
    }

    /**
     * 修改/新增设备
     *
     * @param device 设备信息
     * @return
     */
    @PostMapping("save")
    public Msg saveDevice(@RequestBody Device device) {

        Msg msg = new Msg(false);

        //设备验证
        if (device == null) {
            msg.setMsg("设备参数信息错误！");
            return msg;
        }

        //保存设备信息
        return deviceService.saveDevice(device);
    }

    /**
     * 删除设备
     *
     * @param device 设备信息
     * @return
     */
    @PostMapping("delete")
    public Msg deleteDevice(@RequestBody Device device) {

        boolean bool = deviceService.deleteDevice(device.getId());
        return new Msg(bool, bool ? "删除设备成功！" : "删除设备失败！");

    }

    @PostMapping("activation")
    public Msg activateZwan(@RequestBody Device device) {
        return deviceService.activation(device);
    }


    @PostMapping("findUsers")
    public List<User> findBindUsers(@RequestBody Device device){
        return deviceService.findBindUsers(device);
    }

    @PostMapping("getAllTypesDevice")
    public List<Device> queryAllTypesDevice(@RequestBody Device device){
        return deviceService.queryAllTypesDevice(device);
    }

    @PostMapping("getSubCompany")
    public List<Company> findSubCompany(@RequestBody Device device){
        return deviceService.findSubCompany(device);
    }

    @PostMapping("plantformId")
    public Integer findPlantformId(){
        return deviceService.findPlantformId();
    }

    @PostMapping("getRealName")
    public String getRealName(){
        return deviceService.getRealName();
    }


}
